#define DEBUG
#include <cstdio>

using namespace std;

typedef long long ll;

const int maxn=80;

ll max(ll a, ll b) {
  return a>b ? a : b;
}

int main() {
  freopen("fortune.in", "r", stdin);
  freopen("fortune.out", "w", stdout);

  static ll x[maxn+1], y[maxn+1], z[maxn+1], f[maxn+1][maxn+1][maxn*maxn+1];
  int num, t, n;
  scanf("%d %d", &num, &t);
  for (int ti=1; ti<=t; ti++) {
    scanf("%d", &n);
    for (int i=1; i<=n; i++) {
      scanf("%lld %lld %lld", x+i, y+i, z+i);
    }

    for (int i=1; i<=n; i++) {
      for (int j=0; j<=n-i; j++) {
	for (int k=j*i; k<=n*(n+1)/2; k++) {
	  f[i][j][k] = max(f[i-1][j][k]+max(y[i]*j, z[i]*(k-j*i)), f[i-1][j+1][k+i]+x[i]);
	}
      }
    }
    printf("%lld\n", f[n][0][0]);
  }
  
  fclose(stdin);
  fclose(stdout);
  return 0;
}
